#include <stdio.h>

// 之所以增加__cplusplus的宏定义，是为了同时兼容gcc编译器和g++编译器
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#ifdef __cplusplus
};
#endif

int main(int argc, char **argv) {
    const char *filename = "../fuzhou.mp4";
    if (argc > 1) {
        filename = argv[1];
    }
    AVFormatContext *fmt_ctx = NULL;
    // 打开音视频文件
    int ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Can't open file %s.\n", filename);
        return -1;
    }
    av_log(NULL, AV_LOG_INFO, "Success open input_file %s.\n", filename);
    // 查找音视频文件中的流信息
    ret = avformat_find_stream_info(fmt_ctx, NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Can't find stream information.\n");
        return -1;
    }
    av_log(NULL, AV_LOG_INFO, "Success find stream information.\n");
    // 格式化输出文件信息
    av_dump_format(fmt_ctx, 0, filename, 0);
    av_log(NULL, AV_LOG_INFO, "duration=%d\n", fmt_ctx->duration); // 持续时间，单位微秒
    av_log(NULL, AV_LOG_INFO, "bit_rate=%d\n", fmt_ctx->bit_rate); // 比特率，单位比特每秒
    av_log(NULL, AV_LOG_INFO, "nb_streams=%d\n", fmt_ctx->nb_streams); // 数据流的数量
    av_log(NULL, AV_LOG_INFO, "max_streams=%d\n", fmt_ctx->max_streams); // 数据流的最大数量
    // 找到视频流的索引
    int video_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    av_log(NULL, AV_LOG_INFO, "video_index=%d\n", video_index);
    if (video_index >= 0) {
        AVStream *video_stream = fmt_ctx->streams[video_index];
        av_log(NULL, AV_LOG_INFO, "video_stream index=%d\n", video_stream->index);
        av_log(NULL, AV_LOG_INFO, "video_stream start_time=%d\n", video_stream->start_time);
        av_log(NULL, AV_LOG_INFO, "video_stream nb_frames=%d\n", video_stream->nb_frames);
        av_log(NULL, AV_LOG_INFO, "video_stream duration=%d\n", video_stream->duration);
    }
    // 找到音频流的索引
    int audio_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    av_log(NULL, AV_LOG_INFO, "audio_index=%d\n", audio_index);
    if (audio_index >= 0) {
        AVStream *audio_stream = fmt_ctx->streams[audio_index];
        av_log(NULL, AV_LOG_INFO, "audio_stream index=%d\n", audio_stream->index);
        av_log(NULL, AV_LOG_INFO, "audio_stream start_time=%d\n", audio_stream->start_time);
        av_log(NULL, AV_LOG_INFO, "audio_stream nb_frames=%d\n", audio_stream->nb_frames);
        av_log(NULL, AV_LOG_INFO, "audio_stream duration=%d\n", audio_stream->duration);
    }
    avformat_close_input(&fmt_ctx); // 关闭音视频文件
    return 0;
}

/*
lubancat@ubuntu:~/桌面/FFmpeg/chapter02$ gcc look.c -o look -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/lib -lavformat -lavdevice -lavfilter -lavcodec -lavutil -lswscale -lswresample -lpostproc -lm
look.c: In function ‘main’:
look.c:36:42: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   36 |     av_log(NULL, AV_LOG_INFO, "duration=%d\n", fmt_ctx->duration); // 持续时间，单位微秒
      |                                         ~^     ~~~~~~~~~~~~~~~~~
      |                                          |            |
      |                                          int          int64_t {aka long int}
      |                                         %ld
look.c:37:42: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   37 |     av_log(NULL, AV_LOG_INFO, "bit_rate=%d\n", fmt_ctx->bit_rate); // 比特率，单位比特每秒
      |                                         ~^     ~~~~~~~~~~~~~~~~~
      |                                          |            |
      |                                          int          int64_t {aka long int}
      |                                         %ld
look.c:46:61: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   46 |         av_log(NULL, AV_LOG_INFO, "video_stream start_time=%d\n", video_stream->start_time);
      |                                                            ~^     ~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                             |                 |
      |                                                             int               int64_t {aka long int}
      |                                                            %ld
look.c:47:60: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   47 |         av_log(NULL, AV_LOG_INFO, "video_stream nb_frames=%d\n", video_stream->nb_frames);
      |                                                           ~^     ~~~~~~~~~~~~~~~~~~~~~~~
      |                                                            |                 |
      |                                                            int               int64_t {aka long int}
      |                                                           %ld
look.c:48:59: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   48 |         av_log(NULL, AV_LOG_INFO, "video_stream duration=%d\n", video_stream->duration);
      |                                                          ~^     ~~~~~~~~~~~~~~~~~~~~~~
      |                                                           |                 |
      |                                                           int               int64_t {aka long int}
      |                                                          %ld
look.c:56:61: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   56 |         av_log(NULL, AV_LOG_INFO, "audio_stream start_time=%d\n", audio_stream->start_time);
      |                                                            ~^     ~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                             |                 |
      |                                                             int               int64_t {aka long int}
      |                                                            %ld
look.c:57:60: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   57 |         av_log(NULL, AV_LOG_INFO, "audio_stream nb_frames=%d\n", audio_stream->nb_frames);
      |                                                           ~^     ~~~~~~~~~~~~~~~~~~~~~~~
      |                                                            |                 |
      |                                                            int               int64_t {aka long int}
      |                                                           %ld
look.c:58:59: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
   58 |         av_log(NULL, AV_LOG_INFO, "audio_stream duration=%d\n", audio_stream->duration);
      |                                                          ~^     ~~~~~~~~~~~~~~~~~~~~~~
      |                                                           |                 |
      |                                                           int               int64_t {aka long int}
      |                                                          %ld
lubancat@ubuntu:~/桌面/FFmpeg/chapter02$ ./look
Success open input_file ../fuzhou.mp4.
Success find stream information.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '../fuzhou.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf59.27.100
  Duration: 00:00:19.12, start: 0.000000, bitrate: 1216 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1440x810 [SAR 1:1 DAR 16:9], 1083 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
duration=19120000
bit_rate=1216884
nb_streams=2
max_streams=1000
video_index=0
video_stream index=0
video_stream start_time=0
video_stream nb_frames=477
video_stream duration=244224
audio_index=1
audio_stream index=1
audio_stream start_time=0
audio_stream nb_frames=820
audio_stream duration=837900
lubancat@ubuntu:~/桌面/FFmpeg/chapter02$ ./look ../2018.mp4
Success open input_file ../2018.mp4.
Success find stream information.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '../2018.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.71.100
  Duration: 00:04:13.36, start: 0.000000, bitrate: 1353 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1440x810 [SAR 1:1 DAR 16:9], 1218 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
duration=253357000
bit_rate=1353135
nb_streams=2
max_streams=1000
video_index=0
video_stream index=0
video_stream start_time=0
video_stream nb_frames=6332
video_stream duration=3241984
audio_index=1
audio_stream index=1
audio_stream start_time=0
audio_stream nb_frames=10912
audio_stream duration=11171985
*/